const express = require("express")
const app = express();

const {tokensign,expiresIn,getRandom} = require("./utils")

//导入生成唯一id的uuid
const { v4: uuidv4 } = require('uuid');

//使用cors解决跨域
const cors = require("cors")
app.use(cors());

//post请求获取数据，让数据在body里面
bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())

//生成token
const jwt = require("jsonwebtoken")

//模拟登录信息数据
const admin = [
    {id:1,username:'admin',password:123456}
]

//模拟学生成绩信息
const studentScoreInfo = [
    {id:1,name:"李白",gender:'男',Chinese:80,Math:90,English:77,count:247},
    {id:2,name:"小乔",gender:'女',Chinese:80,Math:90,English:77,count:247},
    {id:3,name:"赵云",gender:'男',Chinese:80,Math:90,English:77,count:247},
    {id:4,name:"貂蝉",gender:'女',Chinese:80,Math:90,English:77,count:247},
    {id:5,name:"大桥",gender:'女',Chinese:80,Math:90,English:77,count:247},
    {id:6,name:"韩信",gender:'男',Chinese:80,Math:90,English:77,count:247},
    {id:7,name:"不知所踪",gender:'男',Chinese:80,Math:90,English:77,count:247},
    {id:8,name:"不知火舞",gender:'女',Chinese:80,Math:90,English:77,count:247},
]

/* 
TODO 获取验证码接口
*/
app.get("/checkcode",(req,res)=>{
    res.send({code:200,msg:getRandom(1000,9999)})
})

/* 
TODO 注册
*/

app.get("/reg",(req,res)=>{
    //获取前端传入的数据
    const {username,password} = req.query;
    //将账号密码整理成一个对象
    const obj = {id:uuidv4(),username,password}
    //将数据push到数据库
    admin.push(obj)
    //响应
    res.send({code:200,msg:"注册成功"})
})

/* 
TODO 登录 
*/

app.post("/login",(req,res)=>{
    //获取登录的账号密码
    const {username,password} = req.body;
    //判断账号密码在数据库是否存在
    const result = admin.find((item)=>{
        return item.username == username && item.password == password
    })
    //是否登录成功
    if(result){
        //登录成功
        const token = jwt.sign({username:'wenyuan'},tokensign,{expiresIn})
        res.send({code:200,msg:"登录成功",token})
    }else{
        res.send({code:302,msg:'登录失败'})
    }
})
/* 
TODO 获取学生成绩信息
*/
app.get("/getStudentScoreInfo",(req,res)=>{
    //获取前端传过来的token
    const {token} = req.query;
    //校验token
    jwt.verify(token,tokensign,(err,data)=>{
        if(err) res.send({code:301,msg:"token校验失败"})
    })
    //返回前端想要的数据
    res.send({code:200,msg:"获取学生成绩信息成功",data:{studentScoreInfo}})
})

/* 
TODO 添加学生成绩信息
*/
app.post("/addStudentScoreInfo",(req,res)=>{
    //获取前端传入的数据
    const {token,name,gender,Chinese,Math,English} = req.body
     //校验token
     jwt.verify(token,tokensign,(err,data)=>{
        if(err) res.send({code:301,msg:"token校验失败"})
    })
    //将数据整理成对象，id用uuid生成
    const obj = {id:uuidv4(),name,gender,Chinese,Math,English}
    studentScoreInfo.push(obj)
    res.send({code:200,msg:"添加成功"})
})

/* 
TODO 删除学生成绩信息
 */
app.post("/delStudentScoreInfo",(req,res)=>{
    //获取前端传入的数据
    const {token,id} = req.body;
     //校验token
     jwt.verify(token,tokensign,(err,data)=>{
        if(err) res.send({code:301,msg:"token校验失败"})
    })
    //根据id找下标
    const index =  studentScoreInfo.findIndex((item)=>{
        return item.id == id
    })
    //根据id删除对应的数据
    studentScoreInfo.splice(index,1)
    res.send({code:200,msg:"删除成功"})
})

/* 
TODO 根据Id获取学生成绩信息
*/

app.get("/getStudentScoreInfoById",(req,res)=>{
    //获取前端传入的数据
    const {token,id} = req.query;
     //校验token
     jwt.verify(token,tokensign,(err,data)=>{
        if(err) res.send({code:301,msg:"token校验失败"})
    })
    //根据id、获取学生成绩信息
    const data =  studentScoreInfo.find((item)=>{
        return item.id == id
    })
    //响应
    res.send({code:200,msg:"根据id获取学生成绩信息成功",data})
})

/* 
TODO 修改学生信息
 */
app.post("/upddateStudentScoreInfo",(req,res)=>{
    //获取前端传入的数据
    const {token,id,name,gender,Chinese,Math,English} = req.body
     //校验token
     jwt.verify(token,tokensign,(err,data)=>{
        if(err) res.send({code:301,msg:"token校验失败"})
    })
    //根据id找到对应的下标
    const index =  studentScoreInfo.findIndex((item)=>{
        return item.id == id
    })
    //将前端传入的数据整理成对象
    const obj= {id,name,gender,Chinese,Math,English}
    //替换数据
    studentScoreInfo.splice(index,1,obj)
    //响应
    res.send({code:200,msg:"修改成功"})
})




//开放静态资源
app.use(express.static("public"))
app.listen(8080);